package com.diudiutechnology.www.yadiudiu.yadiudiu.main.MyActivity;

import android.content.Context;
import android.content.Intent;
import android.graphics.Paint;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.diudiutechnology.www.yadiudiu.util.http.HttpClient;
import com.diudiutechnology.www.yadiudiu.util.http.JsonTools;
import com.diudiutechnology.www.yadiudiu.util.utils.MyProgressDialog;
import com.diudiutechnology.www.yadiudiu.util.utils.SharedPreferencesUtil;
import com.diudiutechnology.www.yadiudiu.util.utils.Sundry;
import com.diudiutechnology.www.yadiudiu.util.utils.ToastUtils;
import com.diudiutechnology.www.yadiudiu.yadiudiu.mine.MyActivity.ToResetYourPasswordActivity;
import com.diudiutechnology.www.yadiudiu.yadiudiu.model.RegistrationModel;
import com.diudiutechnology.www.yadiudiu.R;

import org.xutils.common.Callback;

import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import cn.jpush.android.api.JPushInterface;

/**
 * Created by Administrator on 2017/8/16.
 * 登录
 */

public class LoginActivity extends BaseActivity implements View.OnClickListener {

    @Bind(R.id.retur)
    LinearLayout retur;
    @Bind(R.id.admin_name)//手机号
    EditText adminName;
    @Bind(R.id.admin_pow)//密码
    EditText adminPow;
    @Bind(R.id.button)//登录
    Button button;
    @Bind(R.id.checkBox)//保存密码
            CheckBox checkBox;

    @Bind(R.id.pag_ren_text)//快捷登录
    TextView pagrentext;
    @Bind(R.id.title_right)//注册
    TextView titleright;
    @Bind(R.id.forget_your_password)//重置密码
    TextView forgetyourpassword;
Context context;
    private MyProgressDialog mDialog;
    public static LoginActivity loginActivity;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        ButterKnife.bind(this);
        loginActivity=this;
        initView();
    }
    private void initView(){
        context=this;
        pagrentext.getPaint().setFlags(Paint.UNDERLINE_TEXT_FLAG|Paint.ANTI_ALIAS_FLAG);
        retur.setOnClickListener(this);
        button.setOnClickListener(this);
        pagrentext.setOnClickListener(this);
        titleright.setOnClickListener(this);
        forgetyourpassword.setOnClickListener(this);
        SharedPreferencesUtil.putString(SharedPreferencesUtil.Keys.USERID_KEYS,"");//清空userid
        SharedPreferencesUtil.putString(SharedPreferencesUtil.Keys.TOKEN_KEYS,"");//清空token
        String oldString = SharedPreferencesUtil.getString(SharedPreferencesUtil.Keys.PASSWORD_KEYS, "");//获取密码和手机号
        if (!TextUtils.isEmpty(oldString)){
            String[] arr = oldString.split("&");
            adminName.setText(arr[0]);
            adminPow.setText(arr[1]);
        }
        initData();
    }
    private void initData() {

    }

    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.retur:
                finish();
                break;
            case R.id.button:
                if (TextUtils.isEmpty(adminName.getText().toString().trim())){//手机号
                    ToastUtils.show(context, getResources().getString(R.string.please_phone_number));
                    return;
                }
                if (!Sundry.isMobileNO(adminName.getText().toString().trim())){//验证手机号
                    ToastUtils.show(context, getResources().getString(R.string.please_correctness_phone_number));
                    return;
                }
                if (TextUtils.isEmpty(adminPow.getText().toString().trim())){//密码
                    ToastUtils.show(context, getResources().getString(R.string.please_login_password));
                    return;
                }
                processLogin();
                break;
            case R.id.pag_ren_text://快捷登录
           startActivity(new Intent(LoginActivity.this,QuickLoginActivity.class));
                break;
            case R.id.title_right://注册
                Intent intent=    new Intent(LoginActivity.this,RegistrationActivity.class);
                intent.putExtra("pos","0");
                startActivity(intent);
                break;
            case R.id.forget_your_password://重置密码
                startActivity(new Intent(LoginActivity.this,ToResetYourPasswordActivity.class));
                break;
        }
    }
    //登录
    private void processLogin(){
        if (Sundry.isOpenNetwork(context)){
                 mDialog =new MyProgressDialog(context);
            System.out.println("stringmd5result"+JPushInterface.getRegistrationID(this));
        HttpClient.login(adminName.getText().toString().trim(), adminPow.getText().toString().trim(),JPushInterface.getRegistrationID(this), new Callback.CommonCallback<String>() {
            @Override
            public void onSuccess(String result) {
                if (mDialog!=null){
                    mDialog.dismiss();
                }
                System.out.println("stringmd5result"+result);
                List<RegistrationModel> getnote = JsonTools.getnote(result);
                if (getnote!=null||getnote.size()>0){
                    String status = getnote.get(0).getStatus();
                    String getMsg = getnote.get(0).getMsg();
                    if (status.equals("200")){
                        String userid = getnote.get(0).getUserid();
                        String token = getnote.get(0).getToken();

                        SharedPreferencesUtil.putString(SharedPreferencesUtil.Keys.USERID_KEYS,userid);
                        SharedPreferencesUtil.putString(SharedPreferencesUtil.Keys.TOKEN_KEYS,token);
                        if (!checkBox.isChecked()){
                        SharedPreferencesUtil.putString(SharedPreferencesUtil.Keys.PASSWORD_KEYS,adminName.getText().toString().trim()+"&"+adminPow.getText().toString().trim());
                        }
                        ToastUtils.show(context, getMsg);
                        finish();
                    }else {
                      ToastUtils.show(context, getMsg);
                    }
                }
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                if (mDialog!=null){
                    mDialog.dismiss();
                }
                System.out.println("stringmd5resultThrowable"+ex.getMessage());
                ToastUtils.Toastshow(context, getResources().getString(R.string.server));
            }

            @Override
            public void onCancelled(CancelledException cex) {
                if (mDialog!=null){
                    mDialog.dismiss();
                }
                System.out.println("stringmd5resultCancelledException"+cex.getMessage());
                ToastUtils.Toastshow(context, getResources().getString(R.string.server));
            }

            @Override
            public void onFinished() {

            }
        });
        }else {
            ToastUtils.Toastshow(context, getResources().getString(R.string.network));
        }
    }
}
